package com.skplanet.rade.controller.web;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.skplanet.rade.dao.StoryTagDAO;
import com.skplanet.rade.domain.Story;
import com.skplanet.rade.domain.User;
import com.skplanet.rade.service.StoryService;
import com.skplanet.rade.service.TagService;
import com.skplanet.rade.service.UserService;
import com.skplanet.rade.util.TransJSON;


@Controller
public class StoryController {

	@Autowired
	private StoryService storyService;
	
	@Autowired
	private TagService tagService;
	
	@Autowired
	private StoryTagDAO storyTagDAO;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping(value="/story/addform", method = RequestMethod.GET)
	public String writeform(){
		
		return "/story/addform";
	}
	
	/**
	 * @author sun park
	 * 
	 * 새글 쓰기 -> 확인 누를시 서버로 story insert 요청
	 * 
	 *  url : http://141.223.91.65:3132/plastory/story/add
	 * 
	 * @param nickname
	 * @param title
	 * @param contents
	 * @param tag1
	 * @param tag2
	 * @param tag3
	 * 
	 * @return 없음 
	 */
	@RequestMapping( value = "/story/add", method = RequestMethod.POST )
	public void add(
			@RequestParam(value="nickname")String name,
			@RequestParam(value="title")String title,
			@RequestParam(value="contents")String contents,
			@RequestParam(value="tag1")String tag1,
			@RequestParam(value="tag2")String tag2,
			@RequestParam(value="tag3")String tag3,
			@RequestParam(value="share", defaultValue = "false")String share,
			Model model
			) {
		Story story = new Story();

		story.setTitle(title);
		story.setContents(contents);
		story.setWriter(name);
		story.setShare(share);
		
		storyService.addStoryTag(story, tag1, tag2, tag3);
		
		User user = userService.getUserFromNickname(name);
		story.setUser(user);
		userService.addStory(user, story);
		
		storyService.addStory(story);
		
		model.addAttribute("response", "OK");
	}
	
	/**
	 * PlaList : 내가 쓴 글이 아닌 글 목록
	 * 
	 * url : http://141.223.91.65:3132/plastory/story/plalist
	 * 
	 * @param num 리턴 받을 글 목록 수
	 * @param name 현재 사용자 nickname
	 * @param model
	 */
	@RequestMapping( value = "/story/plalist", method = RequestMethod.GET, headers = { "Accept=text/html" }  )
	public void plaList(
			@RequestParam(value="num", defaultValue="10", required=false)int num,
			@RequestParam(value="nickname", defaultValue="sun", required=true)String name,
			Model model
			) {
		List<Story> sList = storyService.getPlaStoryList(name);
		sList = storyService.getNumStoryList(sList, num);
		
		model.addAttribute("story", TransJSON.InfoToJSON(sList));
	}
	
	/**
	 * MyList : 내가 쓴 글 목록 
	 * 
	 * url : http://141.223.91.65:3132/plastory/story/plalist
	 * 
	 * @param num 리턴 받을 글 목록 수
	 * @param name 현재 사용자 nickname
	 * @param model
	 */
	@RequestMapping( value = "/story/mylist", method = RequestMethod.GET, headers = { "Accept=text/html" }  )
	public void myList(
			@RequestParam(value="num", defaultValue="10", required=false)int num,
			@RequestParam(value="nickname", defaultValue="sun", required=true)String name,
			Model model) {
		List<Story> sList =storyService.getStoryList(name);
		sList = storyService.getNumStoryList(sList, num);
		
		model.addAttribute("story", TransJSON.InfoToJSON(sList));
	}

	/**
	 * Search : 검색 결과  
	 * 
	 * url : http://141.223.91.65:3132/plastory/story/search
	 * 
	 * @param num 리턴 받을 글 목록 수
	 * @param name 현재 사용자 nickname
	 * @param keyword 검색 tag 
	 * @param myFlag 내 글 검색 옵션
	 * @param plaFlag 공개 글 검색 옵션 
	 * @param model
	 */
	@RequestMapping( value = "/story/search", method = RequestMethod.GET )
	public void search( 
			@RequestParam(value="num", defaultValue="10", required=false)int num,
			@RequestParam(value="nickname", defaultValue="sun", required=true)String name,
			@RequestParam(value="keyword", defaultValue="mysql", required=true)String keyword,
			@RequestParam(value="myFlag", defaultValue="false", required=true)String myFlag,
			@RequestParam(value="plaFlag", defaultValue="true", required=true)String plaFlag,
			Model model
			) {
		List<Story> sList = storyService.getTagStoryList(keyword);
		
		if((plaFlag.equals("false")) && (myFlag.equals("false"))) {
			sList = storyService.getAllStoryList(sList, name);
		} 
		
		else {
			if(myFlag.equals("true")) {
				sList = storyService.getMyStoryList(sList, name);
			} 
			else if(plaFlag.equals("true")) {
				sList = storyService.getPlaStoryList(sList, name);
			}
		}
		model.addAttribute("story", TransJSON.InfoToJSON(sList));
	}
	
	@RequestMapping( value = "/story/modify", method = RequestMethod.GET )
	public void modify( 
			@RequestParam(value="sid", defaultValue="5")long sid,
			@RequestParam(value="nickname", defaultValue="sun", required=true)String name,
			@RequestParam(value="title", defaultValue="sun")String title,
			@RequestParam(value="contents", defaultValue="sun")String contents,
			@RequestParam(value="tag1", defaultValue="qqq")String tag1,
			@RequestParam(value="tag2", defaultValue="vvv")String tag2,
			@RequestParam(value="tag3", defaultValue="aaa")String tag3,
			@RequestParam(value="share", defaultValue = "true")String share,
			Model model
			) {
		Story story = storyService.getStory(sid);
		Story oldStory = story;
			
		story.setTitle(title);
		story.setContents(contents);
		story.setShare(share);
//
//		if (storyService.changeStoryTag(story, tag1, tag2, tag3) == true) {
////			storyService.removeStoryTag(story, tag1, tag2, tag3);
////			
////			storyService.updateStory(story);
////			storyService.addStoryTag(story, tag1, tag2, tag3);
////			model.addAttribute("response", TransJSON.InfoToJSON(story));
//			story.setStoryTags(null);
//			
//			List<StoryTag> stList = new ArrayList<StoryTag>();
//			stList.add(new StoryTag(tag1));
//			stList.add(new StoryTag(tag2));
//			stList.add(new StoryTag(tag3));
//		}
//		if(story != oldStory) {
			storyService.updateStory(story);
//		}
		
		
//		model.addAttribute("response", "OK");
	}
}
